﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;

/// <summary>
/// 非挂载式单例 在第一次访问属性时，完成初始化
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class Singleton<T> where T:class//,new()
{
    private static T instance;
    public static T Instance
    {
        get
        {
            //return new T();
            if(instance==null)
            {
                Type type = typeof(T);
                ConstructorInfo info = type.GetConstructor(
                                    BindingFlags.Instance | BindingFlags.NonPublic,
                                    null,
                                    Type.EmptyTypes,
                                    null);
                if (info != null)
                    instance = info.Invoke(null) as T;
                else
                    Debug.LogError("没有找到私有的构造函数");
            }
            return instance;
        }

    }
    
}
